package com.atwisdom.star.core.bsl.aop;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.actionType.modelClass.consume.ConsumeActionType;
import com.atwisdom.star.common.vo.ActionData;
import com.atwisdom.star.common.vo.ResultInfo;
import com.atwisdom.star.core.bsl.business.Impl.DataChangeBslServiceImpl;
import com.atwisdom.star.core.bsl.business.utils.ConstantInfo;
import com.atwisdom.star.core.dao.pojo.modelClass.consume.ConsumeInstance;
import com.atwisdom.star.core.dsl.modelClass.consume.Impl.ConsumeInstanceDslServiceImpl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Aspect
public class ConsumeInstanceHandler {

    @Autowired
    private DataChangeBslServiceImpl dataChangeBslService;

    @Autowired
    private ConsumeInstanceDslServiceImpl consumeInstanceDslService;
    @Pointcut("@annotation(com.atwisdom.star.core.bsl.aop.ConsumeInstanceAnnotation)")//用于匹配带有注解的方法
    private void pointcut(){
    }

    @Before("pointcut()")
    public void myBefore(JoinPoint joinpoint){
    }

    @After("pointcut()")
    public void myAfter(JoinPoint joinpoint) throws InterruptedException {
    }

    @Around("pointcut()")
    public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        ActionData actionData = (ActionData<JSONObject>)args[0];
        String action = actionData.getAction();
        ConsumeActionType actionType = ConsumeActionType.valueOf(action);
        String code = actionType.getCode();
        this.beforeDealSendData(code, actionData);
        Object proceed = joinPoint.proceed();
        this.afterDealSendData(code,proceed);
        return proceed;
    }

    private void afterDealSendData(String code, Object proceed) {
        if (code.equalsIgnoreCase(ConsumeActionType.CONSUME_INSTANCE_ADD.getCode())) {
            try {
                ResultInfo<JSONObject> resultInfo =  (ResultInfo) proceed;
                JSONObject data = resultInfo.getData();
                dealSendDataForAdd(data);
            }catch (Exception ex){
                System.out.println("新增数据上传ERP失败");
                ex.printStackTrace();
            }
        }
    }

    private void beforeDealSendData(String code, ActionData actionData) {
        if (code.equalsIgnoreCase(ConsumeActionType.CONSUME_INSTANCE_UPDATE_BY_ID.getCode())) {
            try {
                dealSendDataForUpdate(actionData);
            }catch (Exception ex){
                System.out.println("更新数据上传ERP失败");
                ex.printStackTrace();
            }
        }else if(code.equalsIgnoreCase(ConsumeActionType.CONSUME_INSTANCE_DELETE_BY_IDS.getCode())){
            try {
                dealSendDataForDelete(actionData);
            }catch (Exception ex){
                System.out.println("删除数据上传ERP失败");
                ex.printStackTrace();
            }

        }
    }

    private void  dealSendDataForAdd(JSONObject data){
        dataChangeBslService.dealSendDataForInstance(data,"consume", ConstantInfo.OP_TYPE_ADD,false);
    }

    private void  dealSendDataForUpdate(ActionData actionData){
        List<JSONObject> data = actionData.getData();
        JSONObject jsonObject = data.get(0);
        dataChangeBslService.dealSendDataForInstance(jsonObject,"consume",ConstantInfo.OP_TYPE_UPDATE,false);
    }

    private void  dealSendDataForDelete(ActionData actionData){
        List idList = actionData.getIds();
        Long id = actionData.getId();
        if(CollectionUtil.isEmpty(idList)){
            idList = new ArrayList<>();
            idList.add(id);
        }else{
            id=(Long)idList.get(0);
        }
        ConsumeInstance consumeInstance = consumeInstanceDslService.queryInsById(id);
        if(consumeInstance == null){
            return;
        }
        String className = consumeInstance.getClassName();
        boolean need = dataChangeBslService.needTrans(className, "consume", ConstantInfo.OP_TYPE_DELETE);
        if (need) {
            dataChangeBslService.apiSendDataBatchSave(idList, className, "consume", ConstantInfo.OP_TYPE_DELETE,false);
        }
    }
}
